package com.softroad.help.app.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 
 * @brief
 * @author Fanhao.Yi
 * @date 2015年5月5日上午9:45:45
 * @version V1.0
 */
public class TabIconView extends ImageView {

	private Paint mPaint;
	private Bitmap mSelectedIcon;
	private Bitmap mNormalIcon;
	private Rect mSelectedRect;
	private Rect mNormalRect;
	private int mSelectedAlpha = 0;

	public TabIconView(Context context) {
		super(context);
	}

	public TabIconView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public TabIconView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public final void init(int normal, int selected) {
		this.mNormalIcon = createBitmap(normal);
		this.mSelectedIcon = createBitmap(selected);
		 this.mNormalRect = new Rect(0, 0, this.mNormalIcon.getWidth(),
		 this.mNormalIcon.getHeight());
		 this.mSelectedRect = new Rect(0, 0, this.mSelectedIcon.getWidth(),
		 this.mSelectedIcon.getHeight());
		this.mPaint = new Paint(1);
	}

	private Bitmap createBitmap(int resId) {
		return BitmapFactory.decodeResource(getResources(), resId);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (this.mPaint == null) {
			return;
		}
		this.mPaint.setAlpha(255 - this.mSelectedAlpha);
		canvas.drawBitmap(this.mNormalIcon, null, this.mNormalRect, this.mPaint);
		this.mPaint.setAlpha(this.mSelectedAlpha);
		canvas.drawBitmap(this.mSelectedIcon, null, this.mSelectedRect,
				this.mPaint);
	}

	public final void changeSelectedAlpha(int alpha) {
		this.mSelectedAlpha = alpha;
		invalidate();
	}

	public final void transformPage(float offset) {
		changeSelectedAlpha((int) (255 * (1 - offset)));
	}
}
